/*
 * @Author: tian_biao
 * @Date: 2021-08-04 13:51:42
 * @LastEditTime: 2021-08-04 15:28:16
 * @Description: rfid搜索
 */
import { Toast } from 'vant'
import zzNewRfid from './zzNewRfid'
var Rfid = {
    loadingMessage: function (msg) {
        //loading提示
        Toast.loading({
            duration: 0,
            message: msg,
            forbidClick: true,
            overlay: true
        })
    },
    /*
     * 只返回rfid
     */
    searchRfid: function (success, fail) {
        /*从智能感知服务中获取rfid 
              gzStatus true智能感觉服务开启 false服务关闭
         * */
        if (localStorage.getItem('gzStatus') == 'true') {
            // Utils.Animate.loadBox("正在搜索可用设备...");
            Rfid.loadingMessage("正在搜索可用设备...")
            //获取当前时间
            var nowTime = new Date().getTime()
            var time = function () {
                var rfidList = JSON.parse(localStorage.getItem("scanDevices"))
                if (rfidList != "null" && rfidList && rfidList[0].gatherTime > nowTime) {
                    clearInterval(setRfidEran)
                    Toast.clear();
                    var temp = []
                    for (var i = 0; i < rfidList.length; i++) {
                        temp.push(arr[i].deviceId)
                    }
                    success(temp[0])
                } else {
                    /*定时器走的时间*/
                    var timeInterval = !!(new Date().getTime() - nowTime >= 5000)
                    console.log("nowTime" + nowTime)
                    console.log("new Date().getTime()" + new Date().getTime())
                    console.log("timeInterval" + timeInterval)
                    if (timeInterval) {
                        Toast.clear();
                        // Dialog.showInfo("搜索不到可用设备！", 1, {
                        //     prompts: "提示",
                        //     confirmTxt: "确认",
                        //     cancel: "取消"
                        // }, function () { }, null, true);

                        Dialog.confirm({
                            title: '提示',
                            message: '搜索不到可用设备！',
                        })
                            .then(() => {
                                // on confirm
                            })
                            .catch(() => {
                                // on cancel
                            });
                        if (fail) {
                            fail()
                        }
                        clearInterval(setRfidEran)
                    }
                }
            }
            var setRfidEran = setInterval(time, 1000)
        } else {
            Rfid.loadingMessage("正在搜索可用设备...")
            zzNewRfid.searchRfid(success, fail)
        }
    },

    /**
     * 单次搜索
     */
    searchOnce: function (success, fail) {
        console.log('gzStatus' + localStorage.getItem('gzStatus') + typeof localStorage.getItem('gzStatus'))
        if (localStorage.getItem('gzStatus') == 'true') {
            Rfid.loadingMessage("正在搜索可用设备...")
            //获取当前时间
            var time = function () {
                var rfidList = JSON.parse(localStorage.getItem("scanDevices"))
                if (rfidList != "null" && rfidList && rfidList[0].gatherTime > nowTime) {
                    clearInterval(setRfidEran)
                    Toast.clear();
                    Rfid.earnRfidFromLocalStorage(success, fail, gatherTime)
                } else {
                    /*定时器走的时间*/
                    var timeInterval = !!(new Date().getTime() - nowTime >= 5000)
                    console.log("nowTime" + nowTime)
                    console.log("new Date().getTime()" + new Date().getTime())
                    console.log("timeInterval" + timeInterval)
                    if (timeInterval) {
                        Toast.clear();
                        Dialog.confirm({
                            title: '提示',
                            message: '搜索不到可用设备！',
                        })
                            .then(() => {
                                // on confirm
                            })
                            .catch(() => {
                                // on cancel
                            });
                        if (fail) {
                            fail()
                        }
                        clearInterval(setRfidEran)
                    }

                }
            }
            //var intertime = localStorage.getItem('intertime')
            var setRfidEran = setInterval(time, 1000)
        } else {
            //智芯设备
            Rfid.loadingMessage("正在搜索可用设备...")
            zzNewRfid.searchOnce(success, fail)
        }
    },

    /**
    * 盘点模式
    */
    continueSearch: function (success, fail) {
        if (localStorage.getItem('gzStatus') == 'true') {
            //Utils.Animate.loadBox("正在搜索可用设备...");
            //获取当前时间
            var nowTime = new Date().getTime()
            var time = function () {
                var rfidList = JSON.parse(localStorage.getItem("scanDevices"))
                if (rfidList != "null" && rfidList && rfidList[0].gatherTime > nowTime) {
                    clearInterval(setRfidEran)
                    Toast.clear();
                    Rfid.earnRfidFromLocalStorage(success, fail, gatherTime)
                } else {
                    /*定时器走的时间*/
                    var timeInterval = !!(new Date().getTime() - nowTime >= 5000)
                    console.log("nowTime" + nowTime)
                    console.log("new Date().getTime()" + new Date().getTime())
                    console.log("timeInterval" + timeInterval)

                    if (timeInterval) {
                        Toast.clear();
                        Dialog.confirm({
                            title: '提示',
                            message: '搜索不到可用设备！',
                        })
                            .then(() => {
                                // on confirm
                            })
                            .catch(() => {
                                // on cancel
                            });
                        console.log("fail" + fail)
                        if (fail) {
                            fail()
                        }
                        clearInterval(setRfidEran)
                    }

                }
            }
            //var intertime = localStorage.getItem('intertime')
            var setRfidEran = setInterval(time, 1000)
        } else {
            //智芯
            zzNewRfid.continueSearch(success, fail)
        }
    },

    /*从loaclstorage中获取rfid*/
    earnRfidFromLocalStorage(success, fail, gatherTime) {
        var arr = JSON.parse(localStorage.getItem("scanDevices"))
        var temp = []
        for (var i = 0; i < arr.length; i++) {
          temp.push({ 'tagId': arr[i].deviceId })
        }
  
        Rfid.getDevices(temp, function (result) {
          console.log(gatherTime)
          result.gatherTime = gatherTime
          success(result)
        }, function () {
          fail()
        })
      }
}
export default {
    Rfid
}
